package com.jh.web.sys;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jh.pojo.IPCamRadarInfo;
import com.jh.service.impl.IPCamRadarInfoSrvImpl;

/**
 * Servlet implementation class AddDeviceServlet
 */
@WebServlet("/AddDeviceServlet")
public class AddDeviceServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    static final Logger logger = LoggerFactory.getLogger(AddDeviceServlet.class);

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddDeviceServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset:utf-8");
        String spName = request.getParameter("spName");
        String spDesc = request.getParameter("spDesc");
        String radarIP = request.getParameter("radarIP");
        String radarPort = request.getParameter("radarPort");
        String ipCamIP = request.getParameter("ipCamIP");
        String ipCamName = request.getParameter("ipCamName");
        String ipCamPwd = request.getParameter("ipCamPwd");
        String ipCamRTSP = request.getParameter("ipCamRTSP");
        System.out.println("spName: " + spName + " spDesc: " + spDesc + " radarIP: " + radarIP + " radarPort: "
                + radarPort + " ipCamIP: " + ipCamIP + " ipCamName: " + ipCamName + " ipCamPwd: " + ipCamPwd);
        System.out.println("ipCamRTSP: " + ipCamRTSP);
        IPCamRadarInfo iri = new IPCamRadarInfo();

        if (spName != null && spName.length() != 0) {
            iri.setSp_name(spName);
        } else {
            String dateStr = String.valueOf(new Date().getTime());
            iri.setSp_name(dateStr);
        }

        iri.setSp_desc(spDesc);

        if (radarIP != null && radarIP.length() != 0) {
            iri.setRadar_ip(radarIP);
        } else {
            String dateStr = String.valueOf(new Date().getTime());
            iri.setRadar_ip(dateStr);
        }

        if (radarPort != null && radarPort.length() != 0) {
            iri.setRadar_port(radarPort);
        } else {
            iri.setRadar_port("6000");
        }

        if (ipCamIP != null && ipCamIP.length() != 0) {
            iri.setIpcam_ip(ipCamIP);
        } else {
            String dateStr = String.valueOf(new Date().getTime());
            iri.setIpcam_ip(dateStr);
        }

        iri.setIpcam_name(ipCamName);
        iri.setIpcam_pwd(ipCamPwd);
        iri.setIpcam_url(ipCamRTSP);
        iri.setRadar_status("离线");
        iri.setIpcam_status("离线");

        System.out.println("add iri: " + iri.toString());
        logger.debug("--->" + "add iri: " + iri.toString());
        IPCamRadarInfoSrvImpl srv = new IPCamRadarInfoSrvImpl();
        boolean rst = srv.add(iri);

        response.getWriter().print(rst); // 给ajax返回值
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
